<?php

require ( "includes/config.php" );
require ( "includes/CGI.php" );
require ( "includes/SQL.php" );

$cgi = new CGI ();
$sql = new SQL ( $DBusername, $DBpassword, $server, $database );

if ( ! $sql->isConnected () )
{
  die ( $DatabaseError );
}

require ( "includes/CSQL.php" ); 
require ( "includes/lang/" . $language_pack . "/index.php" );

?>
<!--
//  comment this out, allows you to use your own stylesheet with propertymax pro 
<link rel="stylesheet" href="includes/lang/<?php print "$language_pack"; ?>/style.css" />
-->
<script language="JavaScript" type="text/javascript">
<!-- Begin
var submitcount=0;
function reset() {
document.emailform.name.value="";
document.emailform.email.value="";
document.emailform.address.value="";
}

function checkFields() {                    
if (                                    
 (document.emailform.name.value=="") ||
 (document.emailform.email.value=="")  ||
 (document.emailform.address.value==""))  
   {
   alert("<?php echo GENERAL_FORM_POPUP_ERROR_MESSAGE; ?>");
   return false;
   }
}
//  End --></script>
<?php
  
  if ( $cgi->getValue ( "op" ) == "SendInformationRequest" )
  {
// Contact Form Spam Check for Website URl's which should not be there
$SpamErrorMessage = "<p align=\"center\"><font color=\"red\">Website URLs are not permitted in the request form.</font></b></p>";
if (preg_match("/http/", "" . $cgi->getValue ( "name" ) . "")) {echo "$SpamErrorMessage"; exit();} 
if (preg_match("/http/", "" . $cgi->getValue ( "email" ) . "")) {echo "$SpamErrorMessage"; exit();} 
if (preg_match("/http/", "" . $cgi->getValue ( "telephone" ) . "")) {echo "$SpamErrorMessage"; exit();} 
if (preg_match("/http/", "" . $cgi->getValue ( "address" ) . "")) {echo "$SpamErrorMessage"; exit();} 
  
    $yoursubject = "".EMAIL_SUBJECT." Cod. " . $cgi->getValue ( "propertyref" );
    $emailtext = "
-----------------------------------------------------------------------------
   ".EMAIL_TEXT_TITLE."
-----------------------------------------------------------------------------
".EMAIL_TEXT_BODY."

".EMAIL_PD."
".EMAIL_PROEPRTY_REF."      " . $cgi->getValue ( "propertyref" ) . "
".EMAIL_PROPERTY_ADDRESS."  " . $cgi->getValue ( "propertyaddress" ) . "
".EMAIL_PROPERTY_PRICE."    ".CURRENCY.""  . $cgi->getValue ( "propertyprice" ) . "

".EMAIL_RD."
".EMAIL_NAME."    " . $cgi->getValue ( "name" ) . "
".EMAIL_EMAIL_ADDRESS."   " . $cgi->getValue ( "email" ) . "
".EMAIL_TELEPHONE."   " . $cgi->getValue ( "telephone" ) . "

" . $cgi->getValue ( "address" ) . "

________________________________
".EMAIL_FOOTER."
";

    $yoursubject = stripslashes ( $yoursubject ); 
    $emailtext = stripslashes ( $emailtext );
    @mail ( $cgi->getValue ( "adminemailaddress" ), $yoursubject, $emailtext,
        "From: " . $cgi->getValue ( "email" ) ); 
?>
<br />
<br />
<br />
<p align="center"><?php echo EMAIL_SUCCESS_MESSAGE; ?></p>
<br />
<p align="center">[<a href="?op=search&po=S"><?php echo FORM_VIEW_ALL_SALE_PROPERTIES; ?></a>]</p>
<br />
<br />
<?php

  } 

  // view more information about the property if requested
  if ( $cgi->getValue ( "op" ) == "mi" )
  {
  // Start of Display Output
    require ( "includes/templates/layout_view.php" );
  // End of Display output


    $arows = $sql->execute ( "SELECT * FROM " . $admin_table . " LIMIT 1",
        SQL_RETURN_ASSOC );
    $arow = $arows [ 0 ];

  
?>
<table width="100%"  align="center" class="lvp_top_nav_menu_bg">
<tr valign="top">
<td><p><?php echo LV_MORE_INFORMATION_TITLE; ?></p></td>
</tr>
</table>
<a name="CONTACT" id="CONTACT"></a>
<p align="center"><?php echo LV_MORE_INFORMATION_TEXT; ?></p>
<br />
<table width="85%" align="center" cellpadding="10">
<tr>
<td valign="top"><p><strong><?php echo $cgi->htmlEncode ( $arow [ "adminbusinessname" ] ); ?></strong><br />
<?php echo $cgi->htmlEncode ( $arow [ "adminaddress" ] ); ?><br />
<?php echo $cgi->htmlEncode ( $arow [ "admintown" ] ); ?><br />
<?php echo $cgi->htmlEncode ( $arow [ "admincounty" ] ); ?><br />
<?php echo $cgi->htmlEncode ( $arow [ "adminpostcode" ] ); ?></p>
<p>Tel: <?php echo $cgi->htmlEncode ( $arow [ "admintelephone" ] ); ?><br />
Fax: <?php echo $cgi->htmlEncode ( $arow [ "adminfax" ] ); ?></p></td>
<td><form method="post" name="emailform" id="emailform" onsubmit="return checkFields()">
<input name="op" type="hidden" value="SendInformationRequest" />
<input name="propertyref" type="hidden" value="<?php echo $cgi->htmlEncode ( $row [ "propertyref" ] ); ?>" />
<input name="propertyaddress" type="hidden" value="<?php echo $cgi->htmlEncode ( $row [ "propertyaddress" ] ); ?>" />
<input name="propertylocation" type="hidden" value="<?php echo $cgi->htmlEncode ( $row [ "propertylocation" ] ); ?>" />
<input name="propertyprice" type="hidden" value="<?php echo $cgi->htmlEncode ( $row [ "propertyprice" ] ); ?>" />
<input name="adminemailaddress" type="hidden" value="<?php
                    echo $cgi->htmlEncode ( $arow [ "adminemailaddress" ] ); ?>" />
<table cellpadding="1" cellspacing="1">
<tr>
<td align="right"><p><?php echo LV_NAME; ?>&nbsp;</p></td>
<td><input type="text" name="name" size="35"
                      maxlength="100" /></td>
</tr>
<tr>
<td align="right"><p><?php echo LV_EMAIL; ?>&nbsp;</p></td>
<td><input type="text" name="email" size="35"
                      maxlength="100" /></td>
</tr>
<tr>
<td align="right"><p><?php echo LV_TELEPHONE; ?>&nbsp;</p></td>
<td><input type="text" name="telephone" size="35"
                      maxlength="100" /></td>
</tr>
<tr>
<td align="right" valign="top"><p><?php echo LV_ADDRESS; ?>&nbsp;</p></td>
<td><textarea name="address" rows="5" cols="35"></textarea></td>
</tr>
<tr>
<td align="right" valign="top">&nbsp;</td>
<td><input name="" type="image" src="<?php print "$LanguageFolder/$language_pack"; ?>/buttons/button-send-request.gif" /></td>
</tr>
</table>
<p>&nbsp;</p>
</form></td>
</tr>
</table>
<?php
  }
  else if ( $cgi->getValue ( "op" ) == "search" )
  {
    $pt = $cgi->getValue ( "pt" );
    $pp = $cgi->getValue ( "pp" );
    $pb = $cgi->getValue ( "pb" );
    $pl = $cgi->getValue ( "pl" );
    $po = $cgi->getValue ( "po" );
    $pp_min = $cgi->getValue ( "pp_min" );
    $pp_max = $cgi->getValue ( "pp_max" );
    $query = "
      SELECT
        p.id                AS id,
        p.propertyoption    AS propertyoption,
        p.propertyprice     AS propertyprice,
        p.propertyaddress   AS propertyaddress,
        pl.propertylocation AS propertylocation,
        ps.propertystatus   AS propertystatus,
        pt.propertytype     AS propertytype,
        p.propertyref       AS propertyref,
        p.propertybedrooms  AS propertybedrooms,
        p.propertyphoto1    AS propertyphoto1,
        p.propertyphoto2    AS propertyphoto2,
        p.propertyphoto3    AS propertyphoto3,
        p.propertyphoto4    AS propertyphoto4,
        p.shortdescription  AS shortdescription,
        p.longdescription   AS longdescription,
		p.virtualtour   AS virtualtour
      FROM
        " . $property_table . " p
        INNER JOIN
        " . $propertytypes_table . " pt ON (p.propertytype=pt.id)
        INNER JOIN
        " . $propertylocations_table . " pl ON (p.propertylocation=pl.id)
        INNER JOIN
        " . $propertystatus_table . " ps ON (p.propertystatus=ps.id)
      WHERE
        p.featuredproperty=1
      ";

    if ( $pt != "" )
      $query .= " AND p.propertytype=" . $sql->quote ( $pt );

    if ( $pp_min != "" )
      $query .= " AND p.propertyprice>=" . $sql->quote ( $pp_min );

    if ( $pp_max != "" )
      $query .= " AND p.propertyprice<=" . $sql->quote ( $pp_max );
     
    if ( $pb != "" )
      $query .= " AND p.propertybedrooms>=" . $sql->quote ( $pb );

    if ( $pl != "" )
      $query .= " AND p.propertylocation=" . $sql->quote ( $pl );

     if ( $po != "" )
      $query .= " AND p.propertyoption=" . $sql->quote ( $po );
    // set the query ordering
    if ( isset ( $property_search_order ) && $property_search_order != "" )
      $query .= "ORDER BY p." . $property_search_order . " " .
           ( "a" == strtolower ( substr ( $property_search_ordering, 0, 1 ) ) ?
             "ASC" : "DESC" );
    
    $results = $sql->execute ( $query, SQL_RETURN_ASSOC );
    
    $total_results = sizeof ( $results );

    if ( $total_results == 0 )
    {
      ?>
<br />
<br />
<p align="center"><?php echo GENERAL_NO_PROPERTIES_FOUND; ?></p>
<br />
<br />
<br />
<?php

    }
if ( $total_results  >0 ) {    
    $total_pages = ceil ( $total_results / $property_search_limit ); //total number of pages

    $page = $cgi->getValue ( "page" );

    if ( ! $page )
      $page = 1;
      
    $offset = ( $page - 1 ) * $property_search_limit; //starting number for displaying results out of DB 
    $max = $offset + $property_search_limit;
    $max = ( $max > $total_results ? $total_results : $max );
    
?>
<p align="center"><?php echo GENERAL_PROPERTIES_FOUND; ?></p>
<br />
<?php
 
    for ( $l = $offset; $l < $max; ++$l )
    { 
      $row = $results [ $l ];
      
      // Start of Display Output
      require ( "includes/templates/layout_results.php" );
      // End of Display output
    }  

    ?>
<div align="center">
<p><?php echo GENERAL_PAGE; ?> -
<?php
    
    if ( $page != 1 )
    { 
    ?>
<a href="<?php echo $_SERVER [ 'PHP_SELF' ]; ?>?op=search&page=1&po=<?php echo $po; ?>&pt=<?php echo $pt; ?>&pp=<?php echo $pp; ?>&pb=<?php echo $pb; ?>&pl=<?php echo $pl; ?>&pp_min=<?php echo $pp_min; ?>&pp_max=<?php echo $pp_max; ?>">&lt;&lt; Voltar </a>&nbsp;&nbsp;-&nbsp;
<?php
      
      $prevpage = $page - 1; 
    }

    $to = ( $page < $total_pages - 2 ? $page + 3 : $total_pages );
    $from = ( $page >= 1 && $page <= 3 ? 1 : $page - 3 );

    for ( $i = $from; $i <= $to; ++$i ) 
    { 
      if ( $i == $total_results )
        $to = $total_results; 
        
      if ( $i != $page )
      { 
        ?>
<a href="?op=search&showold=yes&page=<?php echo $i; ?>&po=<?php echo $po; ?>&pt=<?php echo $pt; ?>&pp=<?php echo $pp; ?>&pb=<?php echo $pb; ?>&pl=<?php echo $pl; ?>&pp_min=<?php echo $pp_min; ?>&pp_max=<?php echo $pp_max; ?>"><?php echo $i; ?></a>
<?php
      } 
      else 
      { 
        ?>
<b>[<?php echo $i; ?>]</b>
<?php
      } 
      
      if ( $i != $total_pages ) 
        ?>
&nbsp;&nbsp;
<?php
    } 

    if ( $page != $total_pages )
    { 
      $nextpage = $page + 1; 

      ?>
&nbsp;-&nbsp;&nbsp;<a href="?op=search&page=<?php echo $total_pages; ?>&po=<?php echo $po; ?>&pt=<?php echo $pt; ?>&pp=<?php echo $pp; ?>&pb=<?php echo $pb; ?>&pl=<?php echo $pl; ?>&pp_min=<?php echo $pp_min; ?>&pp_max=<?php echo $pp_max; ?>">Pr&oacute;ximo&gt;&gt;</a>
<?php
    }

    ?>
</p>
</div>
<br />
<br />
<?php
  }
}
  else if ( $cgi->getValue ( "sp" ) == "y" )
 {
      // Start of Display Output
      require ( "includes/templates/layout_view_photos.php" );
      // End of Display output
}

  else
  {
    if ( !$cgi->getValue ( "op" ) == "SendInformationRequest" )
  {
    ?>
<br />
<br />
<h2 align="center"><?php echo FORM_TITLE; ?></h2>
<p align="center"><?php echo FORM_SEARCH_TEXT; ?></p>
<?php
     // Start of Search Box Output
    require ( "includes/templates/inc-searchbox.php" );
	 // End of Search Box Output
}
  }

?>
<br />
<p></p>
